home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / billutil.zip / TFORM.ZIP / TFORM.C < prev    next >
C/C++ Source or Header  |  1993-11-13  |  2KB  |  62 lines

  1. /*                                TForm 1.0
  2.     A simple text formatting program that replaces a single ASCII value
  3.     with any number of other ASCII chars.  Features to be added in v2:
  4.         1) search for multiple char (and replace)
  5.         2) support char cmd line args
  6.         3) allow option of outputfile or overwrite
  7.         4) be able to search and replace EOF (right!)
  8.         5) prompt before change option
  9.  
  10.     Bill Menees -- Cup of Fungus Software -- 11/3/93
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #define TEMPNAME "1$2$3$4$.5$6"
  16.  
  17. void main(int argc, char *argv[])
  18. {
  19.     int ch, fromnum, i;
  20.     char buffer[20];
  21.     FILE *textin, *textout;
  22.  
  23.     if(argc<4) {
  24.         printf("TForm 1.0 - A simple text formatter and character search and replace.\n");
  25.         printf("Bill Menees -- (c) Cup of Fungus Software -- 11/3/93\n");
  26.         printf("\nYou pass TForm a filename to format, the ASCII code of the char to\n");
  27.         printf("replace, and any number of characters to replace it with.\n");
  28.         printf("Primarily intended to be used from a batch file.  Output is in file\n");
  29.         printf("%s which can then be copied over the input file and deleted.\n", TEMPNAME);
  30.         printf("\n---Handy ascii values:---\n");
  31.         printf("Tab=09, Linefeed=10, Formfeed=12, CarriageReturn=13, Space=32;\n");
  32.         printf("'0'=48, ..., '9'=57; 'A'=65, ..., 'Z'=90; 'a'=97, ..., 'z'=122;\n");
  33.         printf("\nSPECIAL VALUE FOR DELETION: -1\n");
  34.         printf("\nUsage: tform filename fromnumber tonumber(s)\n");
  35.         printf("\nExample: \"tform test.txt 13 -1\" will delete\n");
  36.         printf("all carriage returns in the file test.txt.\n");
  37.         exit(1); /* too few args */
  38.     }
  39.  
  40.     if((textin=fopen(argv[1],"rb"))==NULL) {
  41.         printf("Unable to open input file %s\n", argv[1]);
  42.         exit(2);
  43.     }
  44.     if((textout=fopen(TEMPNAME,"wb"))==NULL) {
  45.         printf("Unable to open temporary output file\n");
  46.         exit(3);
  47.     }
  48.  
  49.     fromnum=atoi(argv[2]);
  50.     while((ch=fgetc(textin))!=EOF) {    /* won't be able to trap EOF this way */
  51.         if(ch!=fromnum) fputc(ch, textout);
  52.         else for(i=3;i<argc;i++)
  53.                 if((atoi(argv[i])>-1) && (atoi(argv[i])<256)) fputc(atoi(argv[i]), textout);
  54.     }
  55.     fclose(textin);
  56.     fclose(textout);
  57.  /*    sprintf(buffer, "del %s", argv[1]);
  58.     system(buffer);
  59.     rename(TEMPNAME, argv[1]);  if uncommented new will overwrite old */
  60.     exit(0);
  61. }
  62.